﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace TCCT_CTC_CSDLDangVienV1
{
    public partial class frmCanBoPrint : frmBase
    {
        public string SelectedDauMoiID = "";

        public frmCanBoPrint()
        {
            InitializeComponent();
        }

        private void frmCanBoPrint_Load(object sender, EventArgs e)
        {

            ApplyUI();

            txtNgayTrich.Text = DateTime.Now.ToString(MyConst.FORMAT_DATETIME.ddMMyyyy);
            DataTable temDT = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblCanBoPrint");
            txtNguoiTrich_HoTen.Text = temDT.Rows[0]["NguoiTrich_HoTen"].ToString();
            txtNguoiTrich_ChucVu.Text = temDT.Rows[0]["NguoiTrich_ChucVu"].ToString();
            txtCanCuTrich.Text = temDT.Rows[0]["CanCuTrich"].ToString();

            txtCapBac.Text = temDT.Rows[0]["CapBac"].ToString();

        }

        private void ApplyUI()
        {


            UI.TextBox.ApplyDefault(txtNguoiTrich_HoTen);
            UI.TextBox.ApplyDefault(txtNguoiTrich_ChucVu);
            UI.TextBox.ApplyDefault(txtNgayTrich);
            UI.TextBox.ApplyDefault(txtCanCuTrich);
            UI.Button.ApplyAccept(btnAccept);
            UI.Button.ApplyCancel(btnCancel);

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {

            string sql = "Update tblCanBoPrint Set CapBac=@CapBac, NguoiTrich_HoTen=@NguoiTrich_HoTen, NguoiTrich_ChucVu=@NguoiTrich_ChucVu, CanCuTrich=@CanCuTrich ";


            List<SqlParameter> arrParameters = new List<SqlParameter>();
            arrParameters.Add(SqlHelper.CreateParameterObject("@NguoiTrich_HoTen", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiTrich_HoTen.Text.Trim()));
            arrParameters.Add(SqlHelper.CreateParameterObject("@NguoiTrich_ChucVu", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiTrich_ChucVu.Text.Trim()));
            arrParameters.Add(SqlHelper.CreateParameterObject("@CanCuTrich", SqlDbType.NVarChar, ParameterDirection.Input, txtCanCuTrich.Text.Trim()));
            arrParameters.Add(SqlHelper.CreateParameterObject("@CapBac", SqlDbType.NVarChar, ParameterDirection.Input, txtCapBac.Text.Trim()));

            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParameters);

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void txtCh_Truong_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            MyCommon.MyTuDien(SelectedDauMoiID, sender, MyConst.TD.ChienTr.Ma, MyConst.TD.ChienTr.Ten);//Sửa mã từ đienr truyền vào cho thích hợp
        }

        private void txtCh_Truong_KeyDown(object sender, KeyEventArgs e)
        {
            DevExpress.XtraEditors.ButtonEdit txtSender = (DevExpress.XtraEditors.ButtonEdit)sender;
            if (IsNull(txtSender)) return;
            if (e.KeyCode == Keys.Enter && txtSender.Text.Substring(txtSender.Text.Length - 1).ToString() == Properties.Settings.Default.QuestionChar) //
            {
                MyCommon.MyTuDien(SelectedDauMoiID, sender, MyConst.TD.ChienTr.Ma, MyConst.TD.ChienTr.Ten);//Sửa mã từ đienr truyền vào cho thích hợp
            }
        }

        private void txtCVi_ChDau_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            MyCommon.MyTuDien(SelectedDauMoiID, sender, MyConst.TD.CVCD.Ma, MyConst.TD.CVCD.Ten);//Sửa mã từ đienr truyền vào cho thích hợp
        }

        private void txtCVi_ChDau_KeyDown(object sender, KeyEventArgs e)
        {
            DevExpress.XtraEditors.ButtonEdit txtSender = (DevExpress.XtraEditors.ButtonEdit)sender;
            if (IsNull(txtSender)) return;
            if (e.KeyCode == Keys.Enter && txtSender.Text.Substring(txtSender.Text.Length - 1).ToString() == Properties.Settings.Default.QuestionChar) //
            {
                MyCommon.MyTuDien(SelectedDauMoiID, sender, MyConst.TD.CVCD.Ma, MyConst.TD.CVCD.Ten);//Sửa mã từ đienr truyền vào cho thích hợp
            }
        }

        private void txtThangNam_Leave(object sender, EventArgs e)
        {
            TextEdit txtTem = (TextEdit)sender;
            if (!IsNull(txtTem) && !MyCommon.MyDateTime.IsMonthYear(txtTem.Text))
            {
                txtTem.Text = "";
                MessageBox.Show("Tháng năm không đúng định dạng. ví dụ: 01/2013");
                txtTem.Focus();
            }
        }
    }
}